package com.lineage.server.timecontroller.skill;

import com.lineage.server.model.Instance.L1EffectInstance;
import com.lineage.server.model.Instance.L1EffectType;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1AttackList;
import com.lineage.server.model.L1Character;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldEffect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Random;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EffectFirewallTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(EffectFirewallTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  private static Random _random = new Random();
  
  public void start() {
    int timeMillis = 1650;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1650L, 1650L);
  }
  
  public void run() {
    try {
      Collection<?> allNpc = WorldEffect.get().all();
      if (allNpc.isEmpty())
        return; 
      Iterator<?> iter = allNpc.iterator();
      while (iter.hasNext()) {
        L1EffectInstance effect = (L1EffectInstance)iter.next();
        if (effect.effectType() == L1EffectType.isFirewall) {
          firewall(effect);
          Thread.sleep(1L);
        } 
      } 
    } catch (Exception e) {
      _log.error("Npc L1Effect法师技能(火牢)状态送出时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      EffectFirewallTimer firewallTimer = new EffectFirewallTimer();
      firewallTimer.start();
    } 
  }
  
  private static void firewall(L1EffectInstance effect) {
    try {
      L1PcInstance user = (L1PcInstance)effect.getMaster();
      ArrayList<L1Character> list = WorldEffect.get().getFirewall(effect);
      Iterator<L1Character> iterator = list.iterator();
      while (iterator.hasNext()) {
        L1Character object = iterator.next();
        if (effect.get_showId() == object.get_showId()) {
          if (object instanceof L1PcInstance) {
            L1PcInstance tgpc = (L1PcInstance)object;
            topc(user, tgpc);
            continue;
          } 
          if (!(object instanceof L1MonsterInstance))
            continue; 
          L1MonsterInstance tgmob = (L1MonsterInstance)object;
          tonpc(user, tgmob);
        } 
      } 
    } catch (Exception e) {
      _log.error("Npc L1Effect法师技能(火牢)状态送出时间轴发生异常", e);
      effect.deleteMe();
    } 
  }
  
  private static void tonpc(L1PcInstance user, L1MonsterInstance tgmob) {
    if (dmg0((L1Character)tgmob))
      return; 
    double attrDeffence = 0.0D;
    int weakAttr = tgmob.getFire();
    if (weakAttr > 0)
      attrDeffence = calcAttrResistance(weakAttr); 
    int srcDmg = 19 + _random.nextInt(Math.max(user.getInt() / 2, 1));
    int damage = (int)((1.0D - attrDeffence) * srcDmg);
    damage = Math.max(damage, 0);
    if (damage <= 0)
      return; 
    tgmob.broadcastPacketX10((ServerBasePacket)new S_DoActionGFX(tgmob.getId(), 2));
    tgmob.receiveDamage((L1Character)user, damage);
  }
  
  private static void topc(L1PcInstance user, L1PcInstance tgpc) {
    if (user.getClanid() != 0 && tgpc.getClanid() == user.getClanid())
      return; 
    if (tgpc.isSafetyZone())
      return; 
    if (dmg0((L1Character)tgpc))
      return; 
    double attrDeffence = 0.0D;
    int weakAttr = tgpc.getFire();
    if (weakAttr > 0)
      attrDeffence = calcAttrResistance(weakAttr); 
    int srcDmg = 19 + _random.nextInt(Math.max(user.getInt() / 2, 1));
    int damage = (int)((1.0D - attrDeffence) * srcDmg);
    damage = Math.max(damage, 0);
    boolean dmgX2 = false;
    if (!tgpc.getSkillisEmpty() && tgpc.getSkillEffect().size() > 0)
      try {
        Iterator<Integer> iterator = L1AttackList.SKD3.keySet().iterator();
        while (iterator.hasNext()) {
          Integer skillid = iterator.next();
          if (tgpc.hasSkillEffect(skillid.intValue())) {
            Integer integer = (Integer)L1AttackList.SKD3.get(skillid);
            if (integer == null)
              continue; 
            if (integer.equals(skillid)) {
              dmgX2 = true;
              continue;
            } 
            damage += integer.intValue();
          } 
        } 
      } catch (ConcurrentModificationException concurrentModificationException) {
      
      } catch (Exception e) {
        _log.error(e.getLocalizedMessage(), e);
      }  
    if (dmgX2)
      damage /= 2; 
    if (damage <= 0)
      return; 
    tgpc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(tgpc.getId(), 2));
    tgpc.receiveDamage((L1Character)user, damage, false, true);
  }
  
  private static double calcAttrResistance(int resist) {
    int resistFloor = (int)(0.16D * Math.abs(resist));
    if (resist >= 0) {
      resistFloor *= 1;
    } else {
      resistFloor *= -1;
    } 
    double attrDeffence = resistFloor / 32.0D;
    return attrDeffence;
  }
  
  private static boolean dmg0(L1Character character) {
    try {
      if (character == null)
        return false; 
      if (character.getSkillisEmpty())
        return false; 
      if (character.getSkillEffect().size() <= 0)
        return false; 
      Iterator<Integer> iterator = character.getSkillEffect().iterator();
      while (iterator.hasNext()) {
        Integer key = iterator.next();
        Integer integer = (Integer)L1AttackList.SKM0.get(key);
        if (integer != null)
          return true; 
      } 
    } catch (ConcurrentModificationException concurrentModificationException) {
    
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
    return false;
  }
}
